<?php
class shop_productModel extends model{
	public $base;
	public function __construct(&$base){
		parent::__construct($base);
		$this->table="shop_product";
	}
	
	public function id_list($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['id']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	public function getListByIds($ids){
		if(!empty($ids)){
			$option['where']=" id in("._implode($ids).")";
			$data=parent::select($option);
			 
			if($data){
				$list=array();
				foreach($data as $k=>$v){
					$list[$v['id']]=$v;
				}
				return $list;
			}
		}
		return false;
	}
	
	public function total_num_add($option){
	 
		$shopid=intval($option['shopid']);
		$id=intval($option['id']);
		$total_num=intval($option['total_num']);
		$row=M("shop_product")->selectRow("shopid=".$shopid." AND id=".$id."");
		M("shop_product")->update(array(
				"total_num"=>$row['total_num']+$total_num
		),"id=".$row['id']);	
	}
	
	
	public function statDayAdd($option){
		$option['kday']=$kday=date("Ymd",$option['time']);
		unset($option['time']);
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kday='".$kday."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_day")->selectRow($where);
		if($row){
			
			M("stat_product_day")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum'],
				"money"=>$row['money']+$option['money'],
				"costmoney"=>$row['costmoney']+$option['costmoney'],
				"profitmoney"=>$row['profitmoney']+$option['profitmoney']
			),"id=".$row['id']);
		}else{
			M("stat_product_day")->insert($option);
		}		
		
	}
	
	public function statWeekAdd($option){
		$option['kweek']=$kweek=date("YW",$option['time']);
		unset($option['time']);
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kweek='".$kweek."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_week")->selectRow($where);
		if($row){
			
			M("stat_product_week")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum'],
				"money"=>$row['money']+$option['money'],
				"costmoney"=>$row['costmoney']+$option['costmoney'],
				"profitmoney"=>$row['profitmoney']+$option['profitmoney']
			),"id=".$row['id']);
		}else{
			M("stat_product_week")->insert($option);
		}		
		
	}
	
	public function statMonthAdd($option){
		$option['kmonth']=$kmonth=date("Ym",$option['time']);
		unset($option['time']);
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kmonth='".$kmonth."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_month")->selectRow($where);
		if($row){
			
			M("stat_product_month")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum'],
				"money"=>$row['money']+$option['money'],
				"costmoney"=>$row['costmoney']+$option['costmoney'],
				"profitmoney"=>$row['profitmoney']+$option['profitmoney']
			),"id=".$row['id']);
		}else{
			M("stat_product_month")->insert($option);
		}		
		
	}
}

?>